///<reference path="../../../core/model/CommonDto.ts"/>
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, FormControl, Validators } from '@angular/forms';
import { FormValidatorService } from "../../../core/service/form-validator.service";
import { HttpService } from "../../../core/service/http.service";
import { Register } from "../../../core/model/Register";
import { HttpRes } from "../../../core/model/HttpRes";
import { Router, NavigationExtras } from "@angular/router";
import { AuthService } from "../../../core/service/auth.service";
import { NzMessageService, NzModalRef, NzModalService } from 'ng-zorro-antd';
import { Login } from "../../../core/model/Login";
import { ClassInfoOutput } from "../../../core/model/ClassInfoDto";
import { PagerSearchInput, PagedResultDto, ElementViewType } from "../../../core/model/CommonDto";


@Component({
  selector: 'app-my-drill-add',
  templateUrl: './my-drill-add.compopnent.html',
  styleUrls: ['../../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.scss', './my-drill-add.compopnent.scss']
})
export class StudentDrillAddComponent implements OnInit {


  selectTheme;
  selectType;

  _drillsubject;
  _drillCourse;
  subjectsData;
  _drillCourseSubject;
  typeoptions = [
    { dValue: 0, caption: '自由演练' },
    { dValue: 1, caption: '角色演练' },
    { dValue: 2, caption: '分组演练' }
  ];
  drillType = 0;
  creatorRole;
  drillRoles;
  IsShowdrillRoles = false;
  resultMsg;
  get drillCourseSubject() {
    return this._drillCourseSubject;
  }

  set drillCourseSubject(value) {
    this._drillCourseSubject = value;
    this.subjectsData.forEach(e => {
      if (e.keystr === value) {
        this._drillsubject = e.subjectId;
        this._drillCourse = e.courseId;
      }
    });


    // if (this.drillType ==1||this.drillType ==2) {
    this.getDrillRoles();
    // } else  {
    // }
  }
  signForm: FormGroup;
  constructor(public fb: FormBuilder,
    public FormValidator: FormValidatorService,
    private modal: NzModalService,
    public http: HttpService,
    public router: Router,
    public Auth: AuthService) {
    this.newMethod();
  }

  private newMethod() {
    this.GetMySubject();
  }

  ngOnInit() {
  }

  async GetMySubject() {
    this.subjectsData = new Array();
    const subjects = await this.http.post<any>('Course/GetCourseSubjects', "");
    if (subjects.length !== 0) {
      this.subjectsData = subjects;
      this._drillsubject = this.subjectsData[0].subjectId;
      this._drillCourse = this.subjectsData[0].courseId;
      this.drillCourseSubject = this.subjectsData[0].keystr;
    }
  }
  async getDrillRoles() {
    const roles = await this.http.get<any>('DrillRecord/GetDrillRoles?drillRecordId=' + parseInt(this._drillsubject, 10));
    this.drillRoles = new Array();
    this.drillRoles = roles;
    if (roles.length !== 0) {
      this.drillRoles = roles;
      this.creatorRole = this.drillRoles[0].dValue;
    } else {
      this.creatorRole = null;
    }
  }

  async save() {
    if (this.selectTheme == null ) {
      this.modal.warning({
        nzTitle: '提示',
        nzContent: '请选择演练主题。'
      });
      return;
    }
    if (this.selectType == null) {
      this.modal.warning({
        nzTitle: '提示',
        nzContent: '请选择演练类型。'
      });
      return;
    }
    if (this.drillType === 0) {
      const par = { DrillSubjectId: this._drillsubject, DrillType: this.drillType, CreatorRoleId: null, CourseId: this._drillCourse };
      this.resultMsg = await this.http.post<any>('DrillRecord/SaveDrillRecord', par);
    } else {
      if (this.creatorRole == null) {
        this.modal.warning({
          nzTitle: '提示',
          nzContent: '请选择角色。'
        });
        return;
      }
      const par = {
         DrillSubjectId: this._drillsubject, DrillType: this.drillType, CreatorRoleId: this.creatorRole, CourseId: this._drillCourse };
      this.resultMsg = await this.http.post<any>('DrillRecord/SaveDrillRecord', par);
    }

    // 判断是否成功
    switch (this.drillType) {
      case 0: this.router.navigate(['app/mydrill']); break;
      case 1: this.router.navigate(['app/mydrill']); break;
      case 2: this.router.navigate(['app/mydrill']); break;
      default:
    }
  }

  backpage() {
    window.history.go(-1);
  }

  theme(i) {
    this.selectTheme = i;
    this.drillCourseSubject = this.subjectsData[i].keystr;
  }
  type(i) {
    this.selectType = i;
    this.creatorRole = null;
    this.drillType = this.typeoptions[i].dValue;
  }

  roles(role, type) {
    this.selectType = type;
    this.drillType = type;
    this.creatorRole = this.drillRoles[role].dValue;
  }

}
